/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package image.review.components;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Scrollable;
import javax.swing.SwingConstants;

/**
 *
 * @author Troels
 */
public class ImageComponent extends Component implements Scrollable {

    private int maxUnitIncrement = 1;
    private BufferedImage img;
    private File file;

    public ImageComponent() {
    }

    public ImageComponent(File file) {

        this.file = file;

        setImg(file);
    }

    @Override
    public void paint(Graphics g) {

        g.drawImage(img, 0, 0, null);
    }

    public static enum Rotation {

        QUARTER(90),
        HALF(180),
        THREEQUATERS(270);
        private final double angle;

        Rotation(double angle) {
            this.angle = angle;
        }
    };

    public static BufferedImage rotateImage(BufferedImage image, Rotation rotation) {

        Graphics2D g = image.createGraphics();
        g.rotate(-Math.toRadians(rotation.angle));
        return image;
    }
    
    /*
     * Factor R
     */
    public void scale(double r) {

        int height = (int) (img.getHeight() / r);
        int width = (int) (img.getWidth() / r);

        Image scaledImage = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);

        BufferedImage imageBuff = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics g = imageBuff.createGraphics();
        g.drawImage(scaledImage, 0, 0, null);
        g.dispose();

        img = imageBuff;
    }

    public void setImg(BufferedImage img) {
        
        this.img = img;
    }
    
    public void setImg(File file) {

        try {

            img = ImageIO.read(file);
        }
        catch (IOException e) {

            e.printStackTrace();
        }
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public BufferedImage getImg() {
        return img;
    }
    
    @Override
    public Dimension getPreferredSize() {

        if (img != null) {

            return new Dimension(img.getWidth(), img.getHeight());
        }
        else {

            return super.getPreferredSize();
        }
    }

    @Override
    public Dimension getPreferredScrollableViewportSize() {

        return this.getPreferredSize();
    }

    @Override
    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {

        //Get the current position.
        int currentPosition = 0;

        if (orientation == SwingConstants.HORIZONTAL) {

            currentPosition = visibleRect.x;
        }
        else {

            currentPosition = visibleRect.y;
        }

        //Return the number of pixels between currentPosition
        //and the nearest tick mark in the indicated direction.
        if (direction < 0) {

            int newPosition = currentPosition - (currentPosition / maxUnitIncrement) * maxUnitIncrement;
            return (newPosition == 0) ? maxUnitIncrement : newPosition;
        }
        else {
            return ((currentPosition / maxUnitIncrement) + 1) * maxUnitIncrement - currentPosition;
        }
    }

    @Override
    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {

        if (orientation == SwingConstants.HORIZONTAL) {

            return visibleRect.width - maxUnitIncrement;
        }
        else {

            return visibleRect.height - maxUnitIncrement;
        }
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    @Override
    public boolean getScrollableTracksViewportHeight() {
        return false;
    }
}
